
package com.kunbo.cn.controller.core.sale;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.kunbo.cn.util.retu.ReturnBean;

/**
 *
 * @author bjl  
 * @date 2019年3月14日 
 */
@RestController
@RequestMapping("/SaleNotice/")
public class SaleDeliveryNoticeManageController {
	@Autowired
	private JdbcTemplate jdbc;
	@PostMapping("startDnm")
	public ReturnBean startOrder(String tableName ,Integer id,String accruals,String waybill_code) {
		if("".equals(tableName) || null==id) {
			return new ReturnBean(false,"参数有误，请检测");
		}
		Integer r = jdbc.update("update business_delivery_notice_manage set state = '7'  where id = "+id+"");
		
		if(r>0) {
			String accrual= accruals;
			int n=0;
			if(accruals!=null&&!waybill_code.isEmpty()) {
				String sql="";
				sql +="select\r\n" ; 
				sql +="d.capacity_to_be_transported   \r\n";
				sql +="FROM business_waybill_manage  d\r\n" ; 
				sql +="WHERE  d.waybill_code='"+waybill_code+"'\r\n";
				List<Map<String,Object>> list = jdbc.queryForList(sql);
				String t = (String)list.get(0).get("capacity_to_be_transported");
				
				Float t1=Float.parseFloat(t);
				Float t2=Float.parseFloat(accrual);
				Float Amount=t1-t2;
			String	amount=String.valueOf(Amount);
				if(Amount>=0) {
				String sql1 = "UPDATE \r\n" +
				        "business_waybill_manage d \r\n"+
				        " set d.capacity_to_be_transported="+amount+"\r\n "+
				        " WHERE  d.waybill_code='"+waybill_code+"'";
						
						 n=jdbc.update(sql1);
				}
				
				}
			return new ReturnBean(true,"操作成功");
			
		}else {
			return new ReturnBean(false,"执行失败");
		}
		
		
	}
	
	@PostMapping("cancelOrder")
	public ReturnBean cancelOrder(String tableName ,Integer id,String accruals,String waybill_code,String state) {
		if("".equals(tableName) || null==id) {
			return new ReturnBean(false,"参数有误，请检测");
		}
		Integer r = jdbc.update("update "+tableName+" set state = '5'  where id = "+id+"");
		if(r>0) {
			return new ReturnBean(true,"操作成功");
		}else {
			return new ReturnBean(false,"执行失败");
		}
		
		
		
	}
	
	@PostMapping("finishOrder")
	public ReturnBean finishOrder(String tableName ,Integer id) {
		if("".equals(tableName) || null==id) {
			return new ReturnBean(false,"参数有误，请检测");
		}
		Integer r = jdbc.update("update "+tableName+" set state = '3'  where id = "+id+"");
		if(r>0) {
			return new ReturnBean(true,"操作成功");
		}else {
			return new ReturnBean(false,"执行失败");
		}
		
	}
	@PostMapping("startApp")
	public ReturnBean startApp(String accruals,String waybill_code) {
		if(null==accruals||null==waybill_code) {
			return new ReturnBean(false,"参数有误，请检测");
		}
			String accrual= accruals;
			int n=0;
				String sql="";
				sql +="select\r\n" ; 
				sql +="d.capacity_to_be_transported   \r\n";
				sql +="FROM business_waybill_manage  d\r\n" ; 
				sql +="WHERE  d.waybill_code='"+waybill_code+"'\r\n";
				List<Map<String,Object>> list = jdbc.queryForList(sql);
				String t = (String)list.get(0).get("capacity_to_be_transported");
				
				Float t1=Float.parseFloat(t);
				Float t2=Float.parseFloat(accrual);
				Float Amount=t1-t2;
			String	amount=String.valueOf(Amount);
				if(Amount>=0) {
				String sql1 = "UPDATE \r\n" +
				        "business_waybill_manage d \r\n"+
				        " set d.capacity_to_be_transported="+amount+"\r\n "+
				        " WHERE  d.waybill_code='"+waybill_code+"'";
						 n=jdbc.update(sql1);
				}
				if(n>0) {
			return new ReturnBean(true,"操作成功");
				}else {
					return new ReturnBean(false,"执行失败");
				}
		}
		
		
	
}
